﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.EntityClient;
using System.Data.SqlClient;

namespace Ferreteria
{
    public partial class frmConeccionDB : Form
    {
        const string datasource = "DISANCO-SERVER\\SQLEXPRESS";
        const string initialcatalog = "Disanco";
        const string userid = "disanco";
        const string password = "d1s4nc0";

        Configuration config;
        ConnectionStringSettings conDisanco;

        public frmConeccionDB()
        {
            InitializeComponent();

            leerDataSource();
        }

        void leerDataSource()
        {
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            conDisanco = config.ConnectionStrings.ConnectionStrings["DisancoEntities"];

            EntityConnectionStringBuilder ecsbConexionActual = new EntityConnectionStringBuilder(conDisanco.ConnectionString);
            SqlConnectionStringBuilder nuevostr = new SqlConnectionStringBuilder(ecsbConexionActual.ProviderConnectionString);

            txbServer.Text = nuevostr.DataSource;
            txbBD.Text = nuevostr.InitialCatalog;
            txbUsuario.Text = nuevostr.UserID;
            txbPass.Text = nuevostr.Password;
        }

        private void btnAceptar_Click(object sender, EventArgs e)
        {
            config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            conDisanco = config.ConnectionStrings.ConnectionStrings["DisancoEntities"];

            EntityConnectionStringBuilder ecsbConexionActual = new EntityConnectionStringBuilder(conDisanco.ConnectionString);
            SqlConnectionStringBuilder nuevostr = new SqlConnectionStringBuilder(ecsbConexionActual.ProviderConnectionString);

            nuevostr.DataSource = txbServer.Text;
            nuevostr.InitialCatalog = txbBD.Text;
            nuevostr.UserID = txbUsuario.Text;
            nuevostr.Password = txbPass.Text;
            ecsbConexionActual.ProviderConnectionString = nuevostr.ConnectionString;
            conDisanco.ConnectionString = ecsbConexionActual.ConnectionString;

            config.Save();

            //Clases.Funcion.ProtectSection("connectionStrings");

            Ferreteria.Clases.Constantes.cadenaConexion = ecsbConexionActual.ConnectionString;

            this.Close();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            txbServer.Text = datasource;
            txbBD.Text = initialcatalog;
            txbUsuario.Text = userid;
            txbPass.Text = password;
        }

        private void btnProbarConexion_Click(object sender, EventArgs e)
        {
            try
            {
                EntityConnectionStringBuilder ecsbConexionActual = new EntityConnectionStringBuilder(conDisanco.ConnectionString);
                SqlConnectionStringBuilder nuevostr = new SqlConnectionStringBuilder(ecsbConexionActual.ProviderConnectionString);

                nuevostr.DataSource = txbServer.Text;
                nuevostr.InitialCatalog = txbBD.Text;
                nuevostr.UserID = txbUsuario.Text;
                nuevostr.Password = txbPass.Text;
                ecsbConexionActual.ProviderConnectionString = nuevostr.ConnectionString;
                conDisanco.ConnectionString = ecsbConexionActual.ConnectionString;

                DisancoEntities context = new DisancoEntities(ecsbConexionActual.ConnectionString);

                PruebaConexion pr = new PruebaConexion();
                pr.id = 1;
                pr.numero_aleatorio = 1000;
                context.AddToPruebaConexion(pr);
                context.SaveChanges();
                context.DeleteObject(pr);
                context.SaveChanges();

                MessageBox.Show("Prueba de conexión exitosa");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}
